package com.pxst.sys.controller;

import com.pxst.annotations.LogOperate;
import com.pxst.entity.Insert;
import com.pxst.entity.Update;
import com.pxst.resp.ApiResp;
import com.pxst.sys.entity.RoleEntity;
import com.pxst.sys.req.AdminRoleReq;
import com.pxst.sys.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
* <p>
* sys_admin_role 控制器实现
* </p>
*
* @author Administrator
* @since 2023-09-05 10:34:24
*/
@RestController
@RequestMapping("/api/admin/role")
@Api(tags = "角色管理")
public class SysAdminRoleController {

    @Autowired
    private RoleService RoleService;


    /**
     * 新增角色
     * @param adminRoleReq
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增角色")
    @LogOperate(operate = "新增角色", service = RoleService.class, type = "add")
    @PreAuthorize("@ss.hasAnyPermissions('system:role:create')")
    public ApiResp create(@RequestBody @Validated(Insert.class) AdminRoleReq adminRoleReq) {
        return RoleService.create(adminRoleReq);
    }

    /**
     * 修改角色状态
     * @param adminRoleReq
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "修改角色")
    @LogOperate(operate = "修改角色", service = RoleService.class, type = "edit")
//    @PreAuthorize("@ss.hasAnyPermissions('system:role:update')")
    public ApiResp updateById(@RequestBody @Validated(Update.class) AdminRoleReq adminRoleReq) {
        return RoleService.updateRole(adminRoleReq);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色")
    @LogOperate(operate = "删除角色", service = RoleService.class, type = "del")
    @PreAuthorize("@ss.hasAnyPermissions('system:role:delete')")
    public ApiResp removeById(@PathVariable Long id) {
        return RoleService.deleteRole(id);
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询角色列表")
    public ApiResp<List<RoleEntity>> getRoleList() {
        return ApiResp.sucess(RoleService.list());
    }

    /**
     * 查询角色信息和权限列表
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询角色权限列表")
    public ApiResp getRolePermission(@PathVariable Long id, @RequestParam(required = false) String with) {
        if (with != null){
            return ApiResp.sucess(RoleService.permissionsList(id , with));
        }else {
            return ApiResp.sucess(RoleService.getById(id));
        }
    }
}
